package com.huakai.springenv.aspect.profiler;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class TimeProfileAspect {

    @Around("execution(* *.*(..)) && @annotation(com.huakai.springenv.aspect.profiler.DBTimeProfiler)")
    public Object catTransactionAspect(ProceedingJoinPoint joinPoint) throws Throwable{
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        DBTimeProfiler timeProfiler = signature.getMethod().getAnnotation(DBTimeProfiler.class);
        if(timeProfiler == null){
            return joinPoint.proceed();
        }
		String methodName = signature.getMethod().getName();
        DBTimeProfile.enter(signature.getMethod().getDeclaringClass().getSimpleName() + "." + methodName);

        try{
            return joinPoint.proceed();
        }finally{
            DBTimeProfile.release();
        }
    }

}
